package com.newcoder.community.service;

import com.newcoder.community.util.RedisKeyUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DataService {
    @Autowired
    private RedisTemplate redisTemplate;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     //将指定的IP计入UV
    public void recordUV(String ip){
        String key = RedisKeyUtil.getUVKey(simpleDateFormat.format(new Date()));
        redisTemplate.opsForHyperLogLog().add(key,ip);
    }
    //统计指定日期范围内的uv
    public long calcuUV(Date start,Date end){
        if(start==null||end==null){
            throw new IllegalArgumentException("参数不能为空");
        }
        //整理日期范围内key
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        while (!calendar.getTime().after(end)){
            String uvKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(calendar.getTime()));
            list.add(uvKey);
            calendar.add(Calendar.DATE,1);
        }
        //合并这些数据
        String redisKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(start),simpleDateFormat.format(end));
        redisTemplate.opsForHyperLogLog().union(redisKey,list.toArray());
        //返回统计的结果
        return redisTemplate.opsForHyperLogLog().size(redisKey);
    }

    //将指定用户记录到dau
    public void recordDAU(int userId){
        String key = RedisKeyUtil.getDAUKey(simpleDateFormat.format(new Date()));
        redisTemplate.opsForValue().setBit(key,userId,true);
    }
    //统计指定用户的dau
    public long getDAU(Date start,Date end){
        if(start==null||end==null){
            throw new IllegalArgumentException("参数不能为空");
        }
        //整理日期范围内key
        List<byte[]> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        while (!calendar.getTime().after(end)){
            String dauKey = RedisKeyUtil.getDAUKey(simpleDateFormat.format(calendar.getTime()));
            list.add(dauKey.getBytes());
            calendar.add(Calendar.DATE,1);
        }
        //进行OR运算
        return (long)redisTemplate.execute(new RedisCallback() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                  String redisKey = RedisKeyUtil.getDAUKey(simpleDateFormat.format(start),simpleDateFormat.format(end));
                  connection.bitOp(RedisStringCommands.BitOperation.OR,
                          redisKey.getBytes(),list.toArray(new byte[0][0]));
                  return connection.bitCount(redisKey.getBytes());
            }
        });
    }

}
